package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ArtJob {

    @Value("${server.port}")
    private String port;


    // 定时任务
    @XxlJob("artJobHandler415")
    public void artJobHandler(){
        String jobParam = XxlJobHelper.getJobParam();
        System.out.println("任务参数:"+jobParam);
        System.out.println("发布文章的任务......");

    }

    @XxlJob("shardingArtJobHandler")
    public void shardingJobHandler(){

        //分片的参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        System.out.println("shardIndex"+shardIndex);
        System.out.println("shardTotal"+shardTotal);
        //业务逻辑
        List<Integer> list = getList();
        System.out.println("任务总数:"+list.size());
        for (Integer integer : list) {
            if(integer % shardTotal == shardIndex){
                System.out.println("当前第"+shardIndex+"分片执行了，任务项为："+integer);
            }
        }
    }
    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }
        return list;
    }
}